سورس جستجو خطی (++C)
#include
#include
using namespace std;
void insert(int[], int);
int search(int[], int, int);
int main()
{
cout << "********************************************************" << endl;
cout << " smartdevelop.ir" << endl;
cout << "********************************************************" << endl;
int a[100], n, key, result;
cout << "tol ra vared konid : ";
cin >> n;
cout << endl;
insert(a, n);
cout << "\n" << "adad mored nazar ra vared konid:";
cin >> key;
cout << "\n\n";
result = search(a, n, key);
if (result == -1)
cout << "peida nashod " << key << " in array.";
else
{
cout << "paida shod" << "\n\n";
cout << "a[" << result << "]=" << key;
}
cout << "\n\n" << "press any key to exit...";
getch();
return 0;
}
void insert(int a[], int n)
{
for (int i = 0;i<n;i++)
{
cout << "a[" << i << "]=";
cin >> a[i];
}
}
int search(int a[], int n, int key)
{
for (int i = 0;i<n;i++)
{
if (a[i] == key)
return i;
}
return -1;
}
از کتابخانه های conio.h و iostream استفاده کنید
سورس : دانلود
نظرات شما عزیزان: